package com.yf1706.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yf1706.bean.ComprehensiveQuestion;
import com.yf1706.bean.GapFilling;
import com.yf1706.bean.Judge;
import com.yf1706.service.comprehensiveService;
import com.yf1706.service.fillingService;
import com.yf1706.service.judgeService;
import com.yf1706.service.questionBankService;

@Controller
@RequestMapping("exam1")
public class ExamController1 extends BaseController{
	@Autowired
	private questionBankService qbs;
	
	@Autowired
	private fillingService fillservice;
	
	@Autowired
	private judgeService judgeservice;
	
	@Autowired
	private comprehensiveService chservice;
	
	//查询所有的判断题
			@RequestMapping("getAllJudge")
			@ResponseBody
			public Map<String,Object> getAllJudge(int page,int rows){
				System.out.println("进入判断controller");
				Page<Judge> pages = PageHelper.startPage(page, rows);
				List<Judge> list = judgeservice.queryAllJudge();
				long total = pages.getTotal();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("total", total);
				map.put("rows", list);
				return map;
			}
			
			//
			@RequestMapping("JudgeSearch")
			@ResponseBody
			public  Map<String, Object> JudgeSearch(HttpServletRequest request,int page,int rows){
				Page<Judge> pages = PageHelper.startPage(page, rows);
				String value=request.getParameter("value");
				String[] val=value.split(",");
				int qbId=Integer.parseInt(val[0]);
				String examQuestion=val[1];
				System.out.println(qbId+" "+examQuestion);
				Map<String, Object> map = new HashMap<String, Object>();
				long total = pages.getTotal();
				map.put("total", total);
				int a=0;
				if(examQuestion.equals("error")) {
					a=1;
				}
				if(a==1&&qbId==0) {
					
					List<Judge> list=judgeservice.queryAllJudge();
					map.put("rows", list);
					return map;
					
				}else if(qbId!=0&&a==1) {
					List<Judge> list=judgeservice.queryAllByQbIdAndExamQuestion(qbId,"");
					map.put("rows", list);
					return map;
				}else {
					List<Judge> list=judgeservice.queryAllByQbIdAndExamQuestion(qbId,examQuestion);
					map.put("rows", list);
					return map;
				}
				
			}
			//判断表中删除
			@RequestMapping("deleteJudge")
			@ResponseBody
			public  void deleteJudge(HttpServletRequest request){
				String value=request.getParameter("value");
				String[] val=value.split(",");
				
				System.err.println(val.length);
				for(int i=0;i<val.length;i++) {
					int JudgeId=Integer.parseInt(val[i]);
					System.err.println(JudgeId);
					int qbId=judgeservice.findByJudgeId(JudgeId).get(0).getQuestionBank().getQbId();
					System.out.println(qbId);
					qbs.deleteJudgeByQbId(qbId);
					qbId=0;
					judgeservice.deletJudgeById(JudgeId);
					
				}
				
				
			}
			
			
			//在判断按钮界面新增判断题
			@RequestMapping("JudgeAdd")
			@ResponseBody
			public void JudgeAdd(HttpServletRequest request) {
				String value=request.getParameter("qbId");
				String answer=request.getParameter("answer");
				String examQuestion=request.getParameter("examQuestion");
				int qbid=Integer.parseInt(value);
				judgeservice.add(examQuestion,answer,qbid,getSessionUserName(request));
				qbs.ChangeJudgeNum(qbid);
			}
			
			//.......................................................
			
			//查询所有的填空题
			@RequestMapping("getAllFilling")
			@ResponseBody
			public Map<String,Object> getAllFilling(int page,int rows){
				System.out.println("进入判断controller");
				Page<GapFilling> pages = PageHelper.startPage(page, rows);
				List<GapFilling> list = fillservice.queryAllFilling();
				long total = pages.getTotal();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("total", total);
				map.put("rows", list);
				return map;
			}
			
			//
			@RequestMapping("FillingSearch")
			@ResponseBody
			public  Map<String, Object> FillingSearch(HttpServletRequest request,int page,int rows){
				Page<GapFilling> pages = PageHelper.startPage(page, rows);
				String value=request.getParameter("value");
				String[] val=value.split(",");
				int qbId=Integer.parseInt(val[0]);
				String examQuestion=val[1];
				System.out.println(qbId+" "+examQuestion);
				Map<String, Object> map = new HashMap<String, Object>();
				long total = pages.getTotal();
				map.put("total", total);
				int a=0;
				if(examQuestion.equals("error")) {
					a=1;
				}
				if(a==1&&qbId==0) {
					
					List<GapFilling> list=fillservice.queryAllFilling();
					map.put("rows", list);
					return map;
					
				}else if(qbId!=0&&a==1) {
					List<GapFilling> list=fillservice.queryAllByQbIdAndExamQuestion(qbId,"");
					map.put("rows", list);
					return map;
				}else {
					List<GapFilling> list=fillservice.queryAllByQbIdAndExamQuestion(qbId,examQuestion);
					map.put("rows", list);
					return map;
				}
				
			}
			//填空表中删除
			@RequestMapping("deleteFilling")
			@ResponseBody
			public  void deleteFilling(HttpServletRequest request){
				String value=request.getParameter("value");
				String[] val=value.split(",");
				
				System.err.println(val.length);
				for(int i=0;i<val.length;i++) {
					int FillingId=Integer.parseInt(val[i]);
					System.err.println(FillingId);
					int qbId=fillservice.findByGapFillingId(FillingId).get(0).getQuestionBank().getQbId();
					qbs.deleteGapFillingByQbId(qbId);
					fillservice.deleteQuestionBankFilling(FillingId);
					
				}
				
				
			}
			
			
			//在判断按钮界面新增填空题
			@RequestMapping("FillingAdd")
			@ResponseBody
			public void FillingAdd(HttpServletRequest request) {
				String value=request.getParameter("qbId");
				String answer=request.getParameter("answer");
				String examQuestion=request.getParameter("examQuestion");
				int qbid=Integer.parseInt(value);
				fillservice.add(examQuestion,answer,qbid,getSessionUserName(request));
				qbs.ChangeFillingNum(qbid);
			}
	//.........................................................
			//查询所有的综合题
			@RequestMapping("getAllcomprehensive")
			@ResponseBody
			public Map<String,Object> getAllcomprehensive(int page,int rows){
				Page<ComprehensiveQuestion> pages = PageHelper.startPage(page, rows);
				List<ComprehensiveQuestion> list = chservice.queryAllcomprehensive();
				long total = pages.getTotal();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("total", total);
				map.put("rows", list);
				return map;
			}
			
			//
			@RequestMapping("comprehensiveSearch")
			@ResponseBody
			public  Map<String, Object> comprehensiveSearch(HttpServletRequest request,int page,int rows){
				Page<ComprehensiveQuestion> pages = PageHelper.startPage(page, rows);
				String value=request.getParameter("value");
				String[] val=value.split(",");
				int qbId=Integer.parseInt(val[0]);
				String examQuestion=val[1];
				System.out.println(qbId+" "+examQuestion);
				Map<String, Object> map = new HashMap<String, Object>();
				long total = pages.getTotal();
				map.put("total", total);
				int a=0;
				if(examQuestion.equals("error")) {
					a=1;
				}
				if(a==1&&qbId==0) {
					
					List<ComprehensiveQuestion> list=chservice.queryAllcomprehensive();
					map.put("rows", list);
					return map;
					
				}else if(qbId!=0&&a==1) {
					List<ComprehensiveQuestion> list=chservice.queryAllByQbIdAndExamQuestion(qbId,"");
					map.put("rows", list);
					return map;
				}else {
					List<ComprehensiveQuestion> list=chservice.queryAllByQbIdAndExamQuestion(qbId,examQuestion);
					map.put("rows", list);
					return map;
				}
				
			}
			//综合表中删除
			@RequestMapping("deletecomprehensive")
			@ResponseBody
			public  void deletecomprehensive(HttpServletRequest request){
				String value=request.getParameter("value");
				String[] val=value.split(",");
				
				System.err.println(val.length);
				for(int i=0;i<val.length;i++) {
					int comprehensiveId=Integer.parseInt(val[i]);
					System.err.println(comprehensiveId);
					int qbId=chservice.findByComprehensiveQuestionId(comprehensiveId).get(0).getQuestionBank().getQbId();
					qbs.deleteComprehensiveByQbId(qbId);
					chservice.deleteQuestionBankComprehensive(comprehensiveId);
					
				}
				
				
			}
			
			
			//在判断按钮界面新增综合题
			@RequestMapping("comprehensiveAdd")
			@ResponseBody
			public void comprehensiveAdd(HttpServletRequest request) {
				String value=request.getParameter("qbId");
				String answer=request.getParameter("answer");
				String examQuestion=request.getParameter("examQuestion");
				int qbid=Integer.parseInt(value);
				chservice.add(examQuestion,answer,qbid,getSessionUserName(request));
				qbs.ChangeComprehensiveNum(qbid);
			}
			
}
